package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.view.View;
import android.widget.CalendarView;
import android.widget.NumberPicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.PickerView;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/12/11.
 */

public class ApplyReserve extends ObservableWithGoBackHandler {
    private Date selected;
    private String hour;
    private String minute;

    @Bindable
    public Date getSelected() {
        return selected;
    }

    public void setSelected(Date selected) {
        this.selected = selected;
        notifyPropertyChanged(BR.selected);
    }

    public String getMinute() {
        return minute;
    }

    public void setMinute(String minute) {
        this.minute = minute;
    }

    public String getHour() {
        return hour;
    }

    public void setHour(String hour) {
        this.hour = hour;
    }

    public ApplyReserve(Context context) {
        super(context);
        super.getHandler().setTitle("申请成为专家");
    }

    public void onClickSubmit(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                if (WebApi.applyExpert(getContext(), format.format(selected))) {
                    showToast("您的预约时间已经提交");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            HomeActivity.popFragment();
                        }
                    });
                }
            }
        }).start();
    }

    public static void showFragment(Context context) {
        final ApplyReserve reserve = new ApplyReserve(context);
        CommonFragment fragment = new CommonFragment();
        fragment.setDataModel(reserve);
        fragment.setVariableId(BR.reserve);
        fragment.setLayoutId(R.layout.f_apply_reserve);
        reserve.setSelected(Calendar.getInstance().getTime());
        fragment.setInitCallback(new CommonFragment.Callback() {
            @Override
            public void viewCreated(View root) {
//                ListView listView = (ListView)root.findViewById(R.id.timeList);
                Calendar now = Calendar.getInstance();
                now.set(Calendar.HOUR, 9);
                now.set(Calendar.MINUTE, 0);
                reserve.setSelected(now.getTime());
                reserve.setHour("09");
                reserve.setMinute("00");

                CalendarView calendarView = (CalendarView) root.findViewById(R.id.date);
                calendarView.setMinDate(now.getTimeInMillis());

                calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
                    @Override
                    public void onSelectedDayChange(CalendarView calendarView, int i, int i1, int i2) {
                        Calendar date = Calendar.getInstance();
                        date.set(i, i1, i2);
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        String time = String.format(Locale.CHINESE, "%s %s:%s",
                                format.format(date.getTime()),
                                reserve.getHour(), reserve.getMinute());
                        reserve.setSelected(Utils.getDateFromFormat(time, "yyyy-MM-dd HH:mm"));
                    }
                });

                NumberPicker hourPicker = (NumberPicker) root.findViewById(R.id.hourSelector);
                String[] data = new String[13];
                for (int i = 9; i < 22; i++) {
                    data[i - 9] = String.format(Locale.CHINESE, "%02d", i);
                }
                hourPicker.setDisplayedValues(data);
                hourPicker.setMinValue(0);
                hourPicker.setMaxValue(data.length - 1);
                hourPicker.setValue(0);

                NumberPicker minutePicker = (NumberPicker) root.findViewById(R.id.minuteSelector);
                data = new String[60];
                for (int i = 0; i < 60; i++) {
                    data[i] = String.format(Locale.CHINESE, "%02d", i);
                }
                minutePicker.setDisplayedValues(data);
                minutePicker.setMinValue(0);
                minutePicker.setMaxValue(data.length - 1);
                minutePicker.setValue(0);

                hourPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                    @Override
                    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                        String[] data = picker.getDisplayedValues();
                        reserve.setHour(data[newVal]);

                        Date date = reserve.getSelected();
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        String time = String.format(Locale.CHINESE, "%s %s:%s", format.format(date),
                                reserve.getHour(), reserve.getMinute());
                        reserve.setSelected(Utils.getDateFromFormat(time, "yyyy-MM-dd HH:mm"));
                    }
                });

                minutePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                    @Override
                    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                        String[] data = picker.getDisplayedValues();
                        reserve.setMinute(data[newVal]);
                        Date date = reserve.getSelected();
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        String time = String.format(Locale.CHINESE, "%s %s:%s", format.format(date),
                                reserve.getHour(), reserve.getMinute());
                        reserve.setSelected(Utils.getDateFromFormat(time, "yyyy-MM-dd HH:mm"));
                    }
                });
            }
        });
        HomeActivity.popFragment();
        HomeActivity.switchFragment((Activity) context, fragment);
    }
}
